<template>
  <div class="yy-schedule-setting">
    <el-dialog
        draggable
        v-model="isVisible"
        title="设置优先级"
        width="90%"
        :close-on-click-modal="false"
        @close="onCancelDialog"
        :show-close="true"
        class="yy-modal--custom"
        style="max-width: 500px;"
    >
      <div>
        <div style="margin: 15px 0;" v-if="priorityList && priorityList.length">
          <div style="padding: 5px 0;">患者类型:</div>
          <el-select v-model="selectQueueType" clearable placeholder="请选择类型" style="min-width: 120px; flex:1;">
            <el-option :label="item.label" :value="item.value" v-for="item in priorityList" :key="item.value">
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right;"> 排序号: {{ item.sortNo }} </span>
            </el-option>
          </el-select>
        </div>

        <div style="margin: 15px 0;" v-if="selectQueueType === '6'">
          <div style="padding: 5px 0;">绿通类型:</div>
          <el-select v-model="selectLvTong" clearable placeholder="请选择类型" style="min-width: 120px; flex:1;">
            <el-option :label="item.label" :value="item.value" v-for="item in lvtongList" :key="item.value"></el-option>
          </el-select>
        </div>

      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancelDialog">关闭</el-button>
          <el-button type="primary" @click="onSave">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import {defineEmits, defineExpose, onBeforeMount, ref} from "vue";
import NurseClientApi from "@/api/nurse";
import {QueueItemModel} from "@/model/QueueItemModel";

import PriorityApi from "@/api/queue/PriorityApi";
import * as DictDataApi from '@/api/system/dict/data.js'
import {ElMessage} from "element-plus";

  const $emit = defineEmits({
    'changeSignSuccess': (res) => res
  })
  const isVisible = ref(false)

  const queueItem = ref()
  const schedules = ref([])

  const selectQueueType = ref()
  const priorityList = ref([])
  const lvtongList = ref([])
  const selectLvTong = ref()

  onBeforeMount(() => {
    listPriority()
    getLvTongList()
  })

  const onShowDialog = (patient: QueueItemModel) => {
    queueItem.value = patient
    const currenSubQueueList = priorityList.value.map(item => item.value)
    if (patient.subQueueFlag && currenSubQueueList.includes(patient.subQueueFlag.toString())) {
      selectQueueType.value = patient.subQueueFlag.toString()
    } else {
      selectQueueType.value = null
    }
    listSchedule()
    isVisible.value = true
  }

  const listPriority = () => {
    PriorityApi.list().then(data => {
      priorityList.value = data ? data.map(item => Object.assign({
        label: item.name,
        value: item.code,
        sortNo: item.sortNo
      })): []
    })
  }

  const getLvTongList = () => {
      DictDataApi.getDicts('queue_priority_green_list').then((res) => {
         if (res?.code === 200) {
            lvtongList.value = res.data.map(item => Object.assign({label: item.dictLabel, value: item.dictValue}))
         }
      })
  }

  const listSchedule = () => {
    NurseClientApi.getTodayEmplyeeByItem(queueItem.value.departId, queueItem.value.itemId).then(res =>{
      schedules.value = res
    })
  }

  const onSave = () => {
    if (selectQueueType.value == '6' && !selectLvTong.value) {
      ElMessage.warning({
        message: '请选择绿通类型',
        showClose: true
      })
      return
    }
    let queueHisId = queueItem.value.state ? queueItem.value.registerId : queueItem.value.id
    NurseClientApi.setPatientToPriority(queueItem.value.doctorId, null, queueItem.value.itemId, queueItem.value.departId, queueHisId, selectQueueType.value, selectQueueType.value === '6' ? selectLvTong.value : null).then((res) => {
      $emit('changeSignSuccess', res)
      onCancelDialog();
    })
  }

  const onCancelDialog = () => {
    if (isVisible.value) {
      selectQueueType.value = ""
    }
    isVisible.value = false
  }

  defineExpose({
    onShowDialog
  })

</script>
<style scoped lang="scss">

</style>
